home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / a_utils / _archvrs / unix / comp430 / compusi.dos < prev    next >
Text File  |  1990-01-17  |  11KB  |  348 lines

  1. /*@H************************ < COMPRESS utility> ****************************
  2. *   $@(#) compusi.dos,v 4.3d 90/01/18 03:00:00 don Release ^                *
  3. *                                                                           *
  4. *   compress : compusi.dos <MsDos support functions>                        *
  5. *                          <AtariST and Amiga support functions>            *
  6. *                          <Turbo C v2.0 support functions>                 *
  7. *   port by  : Donald J. Gloistein                                          *
  8. *                                                                           *
  9. *   Source, Documentation, Object Code:                                     *
  10. *   released to Public Domain. This code is ported from compress v4.0       *
  11. *   release joe.                                                            *
  12. *                                                                           *
  13. *---------------------------  Module Description  --------------------------*
  14. *   Ms and Pc Dos dependent code.                                           *
  15. *                                                                           *
  16. *   Separated out for ease of writing the main module.                      *
  17. *                                                                           *
  18. *--------------------------- Implementation Notes --------------------------*
  19. *                                                                           *
  20. *   compiled with : compress.h compress.fns                                 *
  21. *   linked with   : compress.obj compapi.obj                                *
  22. *   problems:                                                               *
  23. *                                                                           *
  24. *   To use, copy or rename this file to compusi.c and recompile.            *
  25. *   Set the defines in compress.h to reflect the status of your compiler's  *
  26. *   runtime library, allocation type for malloc()'s, and memory models if   *
  27. *   applicable, and your library function call for malloc() and free().     *
  28. *                                                                           *
  29. *                                                                           *
  30. *---------------------------      Author(s)        -------------------------*
  31. *     Initials ---- Name ---------------------------------                  *
  32. *      DjG          Donald J. Gloistein                                     *
  33. *                   Plus many others, see rev.hst file for full list        *
  34. *      Dal          Dale A. Schumacher (Sozobon C port)                     *
  35. *      LvR          Lyle V. Rains, many thanks for improved implementation  *
  36. *                   of the compression and decompression routines.          *
  37. *************************************************************************@H*/
  38.  
  39.  
  40. #include <stdio.h>
  41. #include "compress.h" /* contains the rest of the include file declarations */
  42.  
  43. /* For those who don't have it in libc.a */
  44.  
  45. #ifdef NO_STRCHR
  46. char *strchr(s, c)
  47. char *s;
  48. int c;
  49. {
  50.     while ( *s) {
  51.         if (*s == (char)c)
  52.             return(s);
  53.         else
  54.             s++;
  55.     }
  56.     return(NULL);
  57. }
  58. #endif
  59.  
  60. #ifdef NO_REVSEARCH
  61. char *strrpbrk(str1,str2)
  62. char *str1;   /*string to be searched */
  63. char *str2;   /*chars to search for */
  64. {
  65.     register char *p;
  66.     register int count = 0;
  67.  
  68.     while (*str1){
  69.         str1++;
  70.         count++;
  71.     }
  72.     str1--;
  73.     while (count--) {
  74.         p = str2;
  75.         do {
  76.             if (*str1 == *p)
  77.                 return(str1);
  78.         } while (*p++);
  79.         str1--;
  80.     }
  81.     return (NULL);
  82. }
  83. #endif
  84.  
  85. char *get_program_name(ptr)
  86. char *ptr;
  87. {
  88.     char *cp,*temp;
  89.  
  90.     if ((cp = strchr(ptr,' '))!= NULL)    /* to allow for os/2 argv[0] */
  91.         *cp = '\0';
  92.     cp = name_index(ptr);
  93.     if ((temp = strchr(cp,'.')) != NULL)
  94.         *temp = '\0';
  95.  
  96.     setbinary(stdin);        /* MSDOS & compiler dependent, mode defaults*/
  97.     setbinary(stdout);       /* to text stdin/out--see compress.h*/
  98.                              
  99. /* DjG Don't assume for the OS, force lower case */
  100.     for (temp = cp;*temp; temp++)
  101.         *temp = tolower(*temp);
  102.  
  103.     if(strncmp(cp,"uncomp",6) == 0) {
  104.         do_decomp = 1;
  105.     }
  106.     else
  107.     if(strncmp(cp, "zcat",4) == 0) {
  108.         keep = TRUE;
  109.         zcat_flg = do_decomp = 1;
  110.     }
  111.     return (cp);
  112. }
  113.  
  114. char *name_index(ptr)
  115. char *ptr;
  116. {
  117.     char *p;
  118.  
  119.     p = strrpbrk(ptr,"\\/:");
  120.     return ((p)?++p:ptr);
  121. }
  122.  
  123. int is_z_name(ptr)       /* checks if it is already a z name */
  124. char *ptr;
  125. {
  126.     int len = (int)strlen(ptr) -1;
  127.     return ((ptr[len] == 'z' || ptr[len] == 'Z'));
  128. }
  129.  
  130. int make_z_name(ptr)
  131. char *ptr;
  132. {
  133.     char *ep,*temp;
  134.  
  135.     temp = name_index(ptr);           /* get right most delimiter */
  136.     if ( (ep = strchr(temp,'.'))!=NULL ) {   /* has an extension */
  137.         if (strlen(ep) > 3) {
  138.             endchar[0] = ep[3];
  139.             ep[3] = 'Z';
  140.         }
  141.         else
  142.             strcat(ep,"Z");
  143.     }
  144.     else
  145.         strcat(ptr,".Z");
  146.     return TRUE;
  147. }
  148. void unmake_z_name(ptr)
  149. char *ptr;
  150. {
  151.     register int len = (int)strlen(ptr)-1;
  152.  
  153.     if ((ptr[len] == 'Z' || ptr[len] == 'z') && *endchar)
  154.         ptr[len] = endchar[0];
  155.     else
  156.         ptr[len] = '\0';
  157. }
  158.  
  159. #ifndef NOSIGNAL
  160. #ifdef ISOS2
  161.              /* this is for Microsoft C v5.1 to compile and be bound */
  162.              /* for use with os/2.  The signal function is different */
  163.              /* between protected and real mode                      */
  164.              /* Since we exit, there is no need for SIG_ACK and      */
  165.              /* resetting the handler. But later code may want it    */
  166. SIGTYPE onintr ( )
  167. {
  168.     signal(SIGINT,SIG_IGN);  /* lets make sure no other interupts happen */
  169.     if (!zcat_flg && !keep_error){
  170.         fclose(stdout);
  171.         unlink ( ofname );
  172.     }
  173.     exit ( ERROR );
  174. }
  175.  
  176. SIGTYPE oops ( )    /* wild pointer -- assume bad input */
  177. {
  178.     signal(SIGINT,SIG_IGN);
  179.     if ( do_decomp == 1 )
  180.         fprintf ( stderr, "%s: corrupt input: %s\n",prog_name,ifname);
  181.     if (!zcat_flg && !keep_error){
  182.         fclose(stdout);
  183.         unlink ( ofname );
  184.     }
  185.     exit ( ERROR );
  186. }
  187. #else        /* for non bound applications */
  188. SIGTYPE onintr ( )
  189. {
  190.  
  191.     if (!zcat_flg && !keep_error){
  192.         fclose(stdout);
  193.         unlink ( ofname );
  194.     }
  195.     exit ( ERROR );
  196. }
  197.  
  198. SIGTYPE oops ( )    /* wild pointer -- assume bad input */
  199. {
  200.     if ( do_decomp == 1 )
  201.         fprintf ( stderr, "%s: corrupt input: %s\n",prog_name,ifname);
  202.     if (!zcat_flg && !keep_error){
  203.         fclose(stdout);
  204.         unlink ( ofname );
  205.     }
  206.     exit ( ERROR );
  207. }
  208. #endif
  209. #endif
  210.  
  211. #ifdef MWC
  212. struct utimbuf {time_t a,b;};
  213. #endif
  214. #ifdef __TURBOC__
  215. struct ftime utimbuf;
  216. #endif
  217. #ifdef SOZOBON
  218. #ifndef S_IFREG
  219. #define S_IFREG         (S_IREAD | S_IWRITE)
  220. #endif
  221. #endif
  222.  
  223. int test_file(ifname)  /* test for a good file name and no links */
  224. char *ifname;          /* returns 0 for good file ERROR for bad  */
  225. {
  226.     struct stat statbuf;
  227.     if (stat(ifname, &statbuf)) {       /* Get stat on input file */
  228.         perror(ifname);
  229.         return(ERROR);
  230.     }else if ((statbuf.st_mode & S_IFMT/*0170000*/) != S_IFREG/*0100000*/) {
  231.         fprintf(stderr, "%s -- not a regular file: unchanged",ifname);
  232.         return(ERROR);
  233.         }
  234. /* take out the following, it is a no operation on dos systems */
  235. /* left the code in, in case some dos like systems use it      */
  236. /* define USE_LINKS if your computer uses links                */
  237. #ifndef USE_LINKS
  238.     else if (statbuf.st_nlink > 1) {
  239.         fprintf(stderr, "%s -- has %d other links: unchanged",ifname,
  240.             statbuf.st_nlink - 1);
  241.         return(ERROR);
  242.     }
  243. #endif
  244.    return (0);
  245. }
  246.  
  247. void copystat(ifname, ofname)
  248. char *ifname, *ofname;
  249. {
  250.     struct stat statbuf;
  251.     int mode;
  252. #ifndef __TURBOC__
  253.     time_t timep[2];
  254. #endif
  255.  
  256.     fclose(stdout);
  257.     if (stat(ifname, &statbuf)) {       /* Get stat on input file */
  258.         perror(ifname);
  259.         return;
  260.     }
  261.     if (exit_stat == NOSAVING && (!force)) { /* No compression: remove file.Z */
  262.         if(!quiet)
  263.             fprintf(stderr, " -- no savings -- file unchanged");
  264.     }
  265.     else if (exit_stat == NOMEM){
  266.         if (!quiet)
  267.             fprintf(stderr, " -- file unchanged");
  268.         if (!do_decomp)
  269.             exit(ERROR);
  270.         else
  271.             return;   /* otherwise will unlink outfile */
  272.     }
  273.     else if (exit_stat == OK) {    /* ***** Successful Compression ***** */
  274.         mode = statbuf.st_mode & 07777;
  275.         if (chmod(ofname, mode))        /* Copy modes */
  276.         perror(ofname);
  277. #ifdef __TURBOC__      /* does not have utime */
  278.         getftime(fileno(stdin),&utimbuf);
  279.         freopen(ofname,READ_FILE_TYPE,stdout);
  280.         setftime(fileno(stdout),&utimbuf);
  281.         fclose(stdout);
  282. #else
  283. #ifdef SOZOBON
  284.         utime(ofname, &(statbuf.st_mtime));
  285. #endif
  286.         timep[0] = statbuf.st_atime;
  287.         timep[1] = statbuf.st_mtime;
  288.         utime(ofname, (struct utimbuf *)timep);   /* Update last accessed and modified times */
  289. #endif
  290.     if (!keep){
  291.             fclose(stdin);
  292.             if (unlink(ifname)) /* Remove input file */
  293.                 perror(ifname);
  294.             if(!quiet)
  295.                 fprintf(stderr, " -- replaced with %s", ofname);
  296.         }
  297.         else{
  298.             if(!quiet)
  299.                 fprintf(stderr, " -- compressed to %s", ofname);
  300.         }
  301.         return;     /* Successful return */
  302.     }
  303.  
  304.     /* Unsuccessful return -- one of the tests failed */
  305.     fclose(stdout);
  306.     if (unlink(ofname))
  307.         perror(ofname);
  308. }
  309.  
  310. #ifndef COMPVER
  311. #ifdef MSDOS
  312. #define COMPVER "Msdos"
  313. #else
  314. #define COMPVER "Generic"
  315. #endif
  316. #endif
  317.  
  318. #ifdef COMP40
  319. #define RESET "Adaptive Reset"
  320. #else
  321. #define RESET "No Adaptive Reset"
  322. #endif
  323.  
  324. void version()
  325. {
  326. #ifdef DEBUG
  327.     fprintf(stderr, "%s\nOptions: %s %s DEBUG MAXBITS = %d\n",rcs_ident,COMPVER,
  328.         RESET,MAXBITS);
  329. #else
  330.     fprintf(stderr, "%s\nOptions: %s %s MAXBITS = %d\n",rcs_ident,COMPVER,
  331.     RESET,MAXBITS);
  332. #endif
  333. }
  334.  
  335. ALLOCTYPE FAR *emalloc(x,y)
  336. unsigned int x;
  337. int y;
  338. {
  339.     ALLOCTYPE FAR *p;
  340.     p = (ALLOCTYPE FAR *)ALLOCATE(x,y);
  341.     return(p);
  342. }
  343. void efree(ptr)
  344. ALLOCTYPE FAR *ptr;
  345. {
  346.     FREEIT(ptr);
  347. }
  348.